<?php

class models_UsertoprojectMapper extends models_MapperBase
{
	public static $_dbTable = 'models_DbTable_Usertoproject';

//----------------------------------------------------------------------------------------------------

	protected static function _initItem($oRow)
	{
		if (null === $oRow)
		{
			return null;
		}

		$oItem = new models_Usertoproject();


		if (isset($oRow->id_user))
			$oItem->id_user 	    = $oRow->id_user;
		if (isset($oRow->id_project))
			$oItem->id_project 		= $oRow->id_project;
		if (isset($oRow->start_date))
			$oItem->start_date      = $oRow->start_date;
        if (isset($oRow->end_date))
            $oItem->end_date        = $oRow->end_date;	
        if (isset($oRow->endless))
            $oItem->endless         = $oRow->endless;            		

		return $oItem;
	}

//----------------------------------------------------------------------------------------------------

	public static function getAll($page = 1, $countItemsOnPage = 10)
	{
		$db = self::_getDbTable(self::$_dbTable);
		$select  = $db->select();

		$select->from($db, array('*'));
		$select->setIntegrityCheck(false);

//		$select->order('id DESC');
//		echo $select; die;

		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
		$paginator->setItemCountPerPage($countItemsOnPage)
				  ->setCurrentPageNumber($page);

		$result = array();
		foreach ($paginator as $page)
		{
			$result[] = (object)$page;
		}

		$ret['array'] = $result;
		$ret['paginator'] = $paginator;

		return $ret;
	}

//----------------------------------------------------------------------------------------------------

	public static function findByIdUser($id)
	{
		$DbTable = self::_getDbTable(self::$_dbTable);
		$select = $DbTable->select();
		$select->setIntegrityCheck(false);

		$select->from($DbTable, array('*'));

	  	$select->where('id_user = ?', $id);

//	  	echo $select;die;
	  	$result = $DbTable->fetchRow($select);

	  	return self::_initItem($result);
	}

//----------------------------------------------------------------------------------------------------

    public static function findByIdProject($id)
    {
        $DbTable = self::_getDbTable(self::$_dbTable);
        $select = $DbTable->select();
        $select->setIntegrityCheck(false);

        $select->from($DbTable, array('*'));

        $select->where('id_project = ?', $id);

//      echo $select;die;
        $result = $DbTable->fetchRow($select);

        return self::_initItem($result);
    }
    
//----------------------------------------------------------------------------------------------------

    public static function findByIdUserAndIdProject($idUser, $idProject)
    {
        $DbTable = self::_getDbTable(self::$_dbTable);
        $select = $DbTable->select();
        $select->setIntegrityCheck(false);

        $select->from($DbTable, array('*'));
        
        $select->where('id_user = ?', $idUser);
        $select->where('id_project = ?', $idProject);

//      echo $select;die;
        $result = $DbTable->fetchRow($select);

        return self::_initItem($result);
    }

//----------------------------------------------------------------------------------------------------

	public static function save($data)
	{
		$DbTable = self::_getDbTable(self::$_dbTable);
		$result = $DbTable->insert($data);
		return $result;
	}

//----------------------------------------------------------------------------------------------------

	public static function edit($id, $data)
	{
//		print_r($data);die;

        $db = self::_getDbTable(self::$_dbTable);
        $where['id_user = ?'] =  (int) $data['id_user'];
        $where['id_project = ?'] =  (int) $data['id_project'];
        $update = $db->update($data, $where);

		return $Update;
	}

//----------------------------------------------------------------------------------------------------

    public static function delete($idUser, $idProject)
    {
        $DbTable = self::_getDbTable(self::$_dbTable);
        $Delete  = $DbTable->delete('id_user = ' . $idUser . ' AND id_project = ' . $idProject);

        return $Delete;     
    }   
    
//----------------------------------------------------------------------------------------------------

    public static function getAllDistinct()
    {
        $db = self::_getDbTable(self::$_dbTable);
        $select  = $db->select();

        $select->from($db, array('id_project'));
        $select->setIntegrityCheck(false);

        $select->distinct();
//      echo $select; die;

        $result = $db->fetchAll($select);

        $ret = array(); 
        
        foreach($result->toArray() as $res)
        {
        	$ret[] = $res['id_project'];
        }

        return $ret;
    }
    
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------

}
